package com.gbx.ximalayating.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.gbx.ximalayating.R;

public class ClassifyActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar tool;
    private WebView web;
    private String[] titles = new String[]{"免费听干货，升级涨姿势",  "喜马拉雅商城","欢乐四人斗地主(真人版)", "玩命加载中"};
    private int position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify);
        tool = (Toolbar) findViewById(R.id.tool);

        setSupportActionBar(tool);
        ActionBar actionBar=getSupportActionBar();
        actionBar.setHomeButtonEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        View customView = LayoutInflater.from(this).inflate(R.layout.classify_web_actionbar, null);
        customView.findViewById(R.id.image).setOnClickListener(this);
        customView.findViewById(R.id.textView).setOnClickListener(this);
        customView.findViewById(R.id.btnRight).setOnClickListener(this);
        TextView textView = (TextView) customView.findViewById(R.id.title_tool);


        customView.findViewById(R.id.tv_toolbar).setOnClickListener(this);
        actionBar.setCustomView(customView);
        actionBar.setDisplayShowCustomEnabled(true);
        Intent intent = getIntent();
        position = intent.getIntExtra("position", -1);

        Log.i("1471","传递位置"+position);
        String url = intent.getStringExtra("url");
        Log.i("1471","传递的url"+url);
        web = ((WebView) findViewById(R.id.web));
        if (web != null){
            textView.setText(titles[position]);
            web.setWebViewClient(new WebViewClient());
            web.setWebChromeClient(new WebChromeClient());
            web.loadUrl(url);
            web.loadUrl(url);
            web.reload();
            web.getSettings().setUseWideViewPort(true);
            web.getSettings().setLoadWithOverviewMode(true);
        } else {
            textView.setText(titles[3]);
        }


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && web.canGoBack()) {
            web.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image:
                finish();
                break;
            case R.id.textView:
                break;
            case R.id.btnRight:
                //自定义dialog
                break;
            case R.id.tv_toolbar:
                finish();
                break;
        }
    }
}
